import { configureStore } from '@reduxjs/toolkit';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { combineReducers } from 'redux';
import counterReducer from './counterSlice';

// 1. 配置持久化
const persistConfig = {
  key: 'root',
  storage,
  whitelist: ['counter'], // 要持久化的 reducer 名
};

// 2. 创建 rootReducer
const rootReducer = combineReducers({
  counter: counterReducer,
});

// 3. 创建持久化 reducer
const persistedReducer = persistReducer(persistConfig, rootReducer);

// 4. 创建 store
export const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false,
    }),
});

// 5. 创建 persistor
export const persistor = persistStore(store);
